// utils/time.js
import dayjs from 'dayjs'
export function getTimeRange(timeType) {
  const now = new Date();
  let beginTime = '';
  let endTime = '';
  
  switch(timeType) {
    case 1: // 本周
      const firstDayOfWeek = new Date(now.setDate(now.getDate() - now.getDay() + (now.getDay() === 0 ? -6 : 1)));
      const lastDayOfWeek = new Date(firstDayOfWeek);
      lastDayOfWeek.setDate(firstDayOfWeek.getDate() + 6);
      
      beginTime = dayjs(firstDayOfWeek).format('YYYY-MM-DD 00:00:00');
      endTime = dayjs(lastDayOfWeek).format('YYYY-MM-DD 23:59:59');
      break;
      
    case 2: // 本月
      const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
      const lastDayOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0);
      
      beginTime = dayjs(firstDayOfMonth).format('YYYY-MM-DD 00:00:00');
      endTime = dayjs(lastDayOfMonth).format('YYYY-MM-DD 23:59:59');
      break;
      
    case 3: // 本年
      const firstDayOfYear = new Date(now.getFullYear(), 0, 1);
      const lastDayOfYear = new Date(now.getFullYear(), 11, 31);
      
      beginTime = dayjs(firstDayOfYear).format('YYYY-MM-DD 00:00:00');
      endTime = dayjs(lastDayOfYear).format('YYYY-MM-DD 23:59:59');
      break;
  }
  
  return { beginTime, endTime };
}

/**
 * 获取饼图数据
 *
 * @export
 * @param {number} [radius=1]
 * @returns {*}
 */
export function getPieChartDataSet({
  radius = 42,
  textColor,
  placeholderColor,
  containerColor,
  channelData = []
}){
   // 处理数据，确保name一致
   const processedData = channelData.map(item => ({
    name: item.name,  // 保持原始name
    value: item.value,
    // 添加显示用的labelName，用于图例显示
    labelName: `${item.name}: ${item.value}`
  }));
  return {
    color: getChartListColor(),
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    grid: {
      top: '0',
      right: '0',
    },
    legend: {
      data: channelData.map(item => item.name), // 使用处理后的图例数据
      selectedMode: false,
      itemWidth: 12,
      itemHeight: 4,
      textStyle: {
        fontSize: 12,
        color: placeholderColor,
      },
      left: 'center',
      bottom: '0',
      orient: 'horizontal', // legend 横向布局。
      // 自定义图例文本格式化
      formatter: (name) => {
        const item = processedData.find(d => d.name === name);
        return item ? `${name}: ${item.value}` : name;
      }
    },
    series: [
      {
        name: '渠道统计',
        type: 'pie',
        radius: ['48%', '60%'],
        avoidLabelOverlap: true,
        selectedMode: true,
        silent: false,
        itemStyle: {
          borderColor: containerColor,
          borderWidth: 1,
        },
        label: {
          show: channelData.length > 0,
          position: 'center',
          formatter: () => {
            if (channelData.length === 0) return '暂无数据';
            const total = channelData.reduce((sum, item) => sum + item.value, 0);
            return `{value|${total}}\n{name|总数量}`;
          },
          rich: {
            value: {
              color: textColor,
              fontSize: 28,
              fontWeight: 'normal',
              lineHeight: 46,
            },
            name: {
              color: '#909399',
              fontSize: 12,
              lineHeight: 14,
            },
          },
        },
        emphasis: {
          scale: true,
          label: {
            show: false,
            rich: {
              value: {
                color: textColor,
                fontSize: 28,
                fontWeight: 'normal',
                lineHeight: 46,
              },
              name: {
                color: '#909399',
                fontSize: 14,
                lineHeight: 14,
              },
            },
          },
        },
        labelLine: {
          show: false,
        },
        data:channelData
      },
    ],
  };
}
